package com.sdcqjy.property.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sdcqjy.mylibrary.adapter.SuperRecyclerAdapter;
import com.sdcqjy.property.R;
import com.sdcqjy.property.mode.ListMsgMode;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 磁磁帅 on 2018/3/31.
 * 功能：
 */

public class FragmentListAdapter extends RecyclerView.Adapter {
    public FragmentListAdapter(Context context, List<ListMsgMode> modeList) {
        this.context = context;
        this.modeList = modeList;
        inflater = LayoutInflater.from(context);
        titleInt = new int[]{1, 2, 3, 4, 5};
    }

    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        RecyclerView.ViewHolder viewHolder;
        if (i == 0) {
            View view = inflater.inflate(R.layout.head_main_fragm_list, viewGroup, false);
            viewHolder = new HeadViewHolder(view);
        } else {
            View view = inflater.inflate(R.layout.item_main_fragm_list, viewGroup, false);
            viewHolder = new ViewHolder(view);
        }
        return viewHolder;
    }

    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
        int type = getItemViewType(i);
        if (type == 1) {
            ListMsgMode mode = modeList.get(i - 1);
            ViewHolder holder = (ViewHolder) viewHolder;
            holder.setData(mode);
        } else {
            HeadViewHolder holder = (HeadViewHolder) viewHolder;
            holder.setData();
        }
    }

    public int getItemCount() {
        return modeList.size() + 1;
    }

    public int getItemViewType(int position) {
        return position == 0 ? 0 : 1;
    }

    public FragmentListAdapter setOnAdapterClickListener(OnAdapterClickListener onAdapterClickListener) {
        this.onAdapterClickListener = onAdapterClickListener;
        return this;
    }

    public void setTitleInt(int[] titleInt) {
        for (int i = 0; i < titleInt.length; i++) {
            int i1 = titleInt[i];
            if (i1 <= 0) titleInt[i] = 1;
            else if (i1 > 5) titleInt[i] = 5;
        }
        this.titleInt = titleInt;
        notifyDataSetChanged();
    }

    private Context context;
    private List<ListMsgMode> modeList;
    private LayoutInflater inflater;
    private OnAdapterClickListener onAdapterClickListener;
    private int[] titleInt;

    public class ViewHolder extends SuperRecyclerAdapter.ViewHolder<ListMsgMode> {
        @BindView(R.id.layout1)
        LinearLayout layout1;
        private TextView textViews[] = new TextView[5];
        private int text_light_dark, text_dark;
        private LinearLayout.LayoutParams params1, params2;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            Resources res = context.getResources();
            text_light_dark = res.getColor(R.color.text_light_dark);
            text_dark = res.getColor(R.color.text_dark);
            params1 = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT);
            params1.weight = 66;
            params2 = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT);
            params2.weight = 113;
        }

        public void setData(ListMsgMode mode) {
            super.setData(mode);
            String ss[] = new String[]{mode.documentCode, mode.title, mode.taxPrice, mode.getFinishTime(), mode.projectStatus};
            layout1.removeAllViews();
            for (int i = 0; i < titleInt.length; i++) {
                int i1 = titleInt[i];
                i1 = i1 <= 0 ? 0 : i1 - 1;
                TextView textView = getTextView(i);
                if (i1 == 1) textView.setLayoutParams(params2);
                else textView.setLayoutParams(params1);
                textView.setText(ss[i1]);
                layout1.addView(textView);
            }
        }

        private TextView getTextView(int post) {
            TextView textView;
            if (textViews[post] == null) {
                textView = new TextView(context);
                textView.setTextSize(9);
                textView.setGravity(Gravity.CENTER);
                textView.setBackgroundColor(0xFFf7fbff);
                textView.setSingleLine(true);
                textViews[post] = textView;
            } else {
                textView = textViews[post];
            }
            post = post % 2;
            textView.setTextColor(post == 0 ? text_light_dark : text_dark);
            return textView;
        }

        @OnClick(R.id.layout1)
        public void onViewClicked() {
            if (onAdapterClickListener != null) onAdapterClickListener.onAdapterClicked(mode);
        }
    }

    public class HeadViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.layout1)
        LinearLayout layout1;

        public HeadViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            Resources res = context.getResources();
            text1 = res.getString(R.string.bianhao);
            text2 = res.getString(R.string.xiangmumingcheng);
            text3 = res.getString(R.string.guapaijiage);
            text4 = res.getString(R.string.jiezhiriqi);
            text5 = res.getString(R.string.xiangmuzhuangtai);
            text_light_dark = res.getColor(R.color.text_light_dark);
            text_dark = res.getColor(R.color.text_dark);
            params1 = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT);
            params1.weight = 66;
            params2 = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT);
            params2.weight = 113;
        }

        private String text1, text2, text3, text4, text5;
        private int text_light_dark, text_dark;
        private LinearLayout.LayoutParams params1, params2;
        private TextView textViews[] = new TextView[5];
        private View lines[] = new View[5];

        public void setData() {
            layout1.removeAllViews();
            String ss[] = new String[]{text1, text2, text3, text4, text5};
            for (int i = 0; i < titleInt.length; i++) {
                if (i > 0) {
                    View line = getLine(i);
                    layout1.addView(line);
                }
                int i1 = titleInt[i];
                i1 = i1 <= 0 ? 0 : i1 - 1;
                TextView textView = getTextView(i);
                if (i1 == 1) textView.setLayoutParams(params2);
                else textView.setLayoutParams(params1);
                textView.setText(ss[i1]);
                layout1.addView(textView);
            }
        }

        private TextView getTextView(int post) {
            TextView textView;
            if (textViews[post] == null) {
                textView = new TextView(context);
                textView.setTextSize(12);
                textView.setGravity(Gravity.CENTER);
                textView.setBackgroundColor(0xFFf7fbff);
                textViews[post] = textView;
            } else {
                textView = textViews[post];
            }
            post = post % 2;
            textView.setTextColor(post == 0 ? text_light_dark : text_dark);
            return textView;
        }

        private View getLine(int post) {
            View view;
            if (lines[post] == null) {
                view = new View(context);
                view.setLayoutParams(new ViewGroup.LayoutParams(1, ViewGroup.LayoutParams.MATCH_PARENT));
                view.setBackgroundResource(R.color.line_);
                lines[post] = view;
            } else {
                view = lines[post];
            }
            return view;
        }
    }

    public interface OnAdapterClickListener {
        public void onAdapterClicked(ListMsgMode mode);
    }
}
